<?php
require_once("email/class.mail.php");
class ucenter extends basis
{

	function index(){

        // 最新景点
		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$sceneryinfo = spClass('sceneryinfo');
		$this->scenerys = $sceneryinfo->findAll($conditions,'id DESC',NULL,"8");
        $i=0;
		while( $i <= count($this->scenerys)-1)
		{		
		 $provinceid = $this->scenerys[$i]['provinceid'];  
		 $cityid = $this->scenerys[$i]['cityid'];
		 $areaid = $this->scenerys[$i]['areaid'];

		 $cityinfo = spClass('lib_cityinfo');

		 $conditions = array( 'id' => $provinceid );
		 $provinces[] = $cityinfo->find($conditions);

		 $conditions = array( 'id' => $cityid );
		 $citys[] = $cityinfo->find($conditions);

		 $conditions = array( 'id' => $areaid );
		 $areas[] = $cityinfo->find($conditions);

		  $i++;
		}

		$this->provinces = $provinces;
		$this->citys = $citys;
		$this->areas = $areas;

        // 最新图片
		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$e_user = spClass('pictureinfo'); 
        $this->pictureinfo = $e_user->findAll($conditions,'id DESC',NULL,"8");
        $scenerys_pic = array();
        $i=0;
		while( $i <= count($this->pictureinfo)-1)
		{		  
		  $linkid = $this->pictureinfo[$i]['linkid'];
		  $conditions = array( 'id' => $linkid );
		  $sceneryinfo = spClass('sceneryinfo');
		  $scenerys_pic[] = $sceneryinfo->find($conditions);
		  $i++;
		}
		$this->scenerys_pic = $scenerys_pic;

        // 用户信息
		$id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result["uname"];
		$this->integral = $e_result["integral"];
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 

        // 昵称和头像向导
        if(empty($e_result["uname"])){
          $this->jump(spUrl('ucenter','UserGuide')); // 跳转到首页
        }

		$this -> display('user/UserIndex.html');
	}

    // 用户资料
    function UserData(){
		$lib_user = spClass('lib_user'); 
        $result = $lib_user->findAll($conditions); 
		$this->result = $result; 
		$id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result['uname']; 
		$this->upass = $e_result['upass']; 
		$this->integral = $e_result["integral"];
		$this->realname = $e_result['realname'];
		$this->Phone = $e_result['Phone']; 
		$this->QQ = $e_result['QQ']; 
		$this->avatar = str_replace(".jpg","",$e_result['avatar']);
		$this->Email = $e_result['Email']; 
		$this->Address = $e_result['Address']; 
	    $this->provinceid = $e_result['provinceid']; 
		$this->cityid = $e_result['cityid']; 
		$this->areaid = $e_result['areaid']; 

		$cataObj = spClass("lib_cityinfo");
		$this->resultclass = $cataObj->getCatalogSub();

		$cataObj = spClass("lib_cityinfo");
		$this->resultsub = $cataObj->getCatalogList();

		$this -> display('user/UserData.html');
	}

	 // 用户向导
    function UserGuide(){
		$lib_user = spClass('lib_user'); 
        $result = $lib_user->findAll($conditions); 
		$this->result = $result; 
		$id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result['uname']; 
		$this->upass = $e_result['upass']; 
		$this->integral = $e_result["integral"];
		$this->realname = $e_result['realname'];
		$this->Phone = $e_result['Phone']; 
		$this->QQ = $e_result['QQ']; 
		$this->avatar = str_replace(".jpg","",$e_result['avatar']);
		$this->Email = $e_result['Email']; 
		$this->Address = $e_result['Address']; 
	    $this->provinceid = $e_result['provinceid']; 
		$this->cityid = $e_result['cityid']; 
		$this->areaid = $e_result['areaid']; 

		$cataObj = spClass("lib_cityinfo");
		$this->resultclass = $cataObj->getCatalogSub();

		$cataObj = spClass("lib_cityinfo");
		$this->resultsub = $cataObj->getCatalogList();

		$this -> display('user/UserGuide.html');
	}


    // 用户密码
    function UserPass(){
		$lib_user = spClass('lib_user'); 
        $result = $lib_user->findAll($conditions); 
		$this->result = $result; 
		$id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result['uname']; 
		$this->upass = $e_result['upass']; 
		$this->integral = $e_result["integral"];
		$this->realname = $e_result['realname'];
		$this->Phone = $e_result['Phone']; 
		$this->QQ = $e_result['QQ']; 
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 
		$this->Email = $e_result['Email']; 
		$this->Address = $e_result['Address']; 
	    $this->provinceid = $e_result['provinceid']; 
		$this->cityid = $e_result['cityid']; 
		$this->areaid = $e_result['areaid']; 

		$cataObj = spClass("lib_cityinfo");
		$this->resultclass = $cataObj->getCatalogSub();

		$cataObj = spClass("lib_cityinfo");
		$this->resultsub = $cataObj->getCatalogList();

		$this -> display('user/UserPass.html');
	}

    // 用户中心_游记
	function UserYouji(){

        $id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result["uname"];
		$this->integral = $e_result["integral"];
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 

		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$e_user = spClass('youjiinfo'); 
        $this->youjiinfo = $e_user->spPager($this->spArgs('page', 1), 15)->findAll($conditions);
        $this->pager = $e_user->spPager()->getPager();
        $scenerys = array();
        $i=0;
		while( $i <= count($this->youjiinfo)-1)
		{		  
		  $linkid = $this->youjiinfo[$i]['linkid'];
		  $conditions = array( 'id' => $linkid );
		  $sceneryinfo = spClass('sceneryinfo');
		  $scenerys[] = $sceneryinfo->find($conditions);
		  $i++;
		}
		$this->scenerys = $scenerys;
 
        // 昵称和头像向导
        if(empty($e_result["uname"])){
          $this->jump(spUrl('ucenter','UserGuide')); // 跳转到首页
        }

		$this -> display('user/UserYouji.html');
	}

    // 用户中心_论坛
	function UserAnswers(){

        $id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result["uname"];
		$this->integral = $e_result["integral"];
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 

		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$e_user = spClass('answersinfo'); 
        $this->answersinfo = $e_user->spPager($this->spArgs('page', 1), 15)->findAll($conditions);
        $this->pager = $e_user->spPager()->getPager();
        $scenerys = array();
        $i=0;
		while( $i <= count($this->answersinfo)-1)
		{		  
		  $linkid = $this->answersinfo[$i]['linkid'];
		  $conditions = array( 'id' => $linkid );
		  $sceneryinfo = spClass('sceneryinfo');
		  $scenerys[] = $sceneryinfo->find($conditions);
		  $i++;
		}
		$this->scenerys = $scenerys;

        // 昵称和头像向导
        if(empty($e_result["uname"])){
          $this->jump(spUrl('ucenter','UserGuide')); // 跳转到首页
        }

		$this -> display('user/UserAnswers.html');
	}

    // 用户中心_点评
	function UserComment(){

        $id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result["uname"];
		$this->integral = $e_result["integral"];
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 

		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$e_user = spClass('commentinfo'); 
        $this->commentinfo = $e_user->spPager($this->spArgs('page', 1), 15)->findAll($conditions);
        $this->pager = $e_user->spPager()->getPager();
        $scenerys = array();
        $i=0;
		while( $i <= count($this->commentinfo)-1)
		{		  
		  $linkid = $this->commentinfo[$i]['linkid'];
		  $conditions = array( 'id' => $linkid );
		  $sceneryinfo = spClass('sceneryinfo');
		  $scenerys[] = $sceneryinfo->find($conditions);
		  $i++;
		}
		$this->scenerys = $scenerys;

        // 昵称和头像向导
        if(empty($e_result["uname"])){
          $this->jump(spUrl('ucenter','UserGuide')); // 跳转到首页
        }

		$this -> display('user/UserComment.html');
	}

	    // 用户中心_图片
	function UserPicture(){

        $id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id;
		$this->uname = $e_result["uname"];
		$this->integral = $e_result["integral"];
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 

		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$e_user = spClass('pictureinfo'); 
        $this->pictureinfo = $e_user->spPager($this->spArgs('page', 1), 15)->findAll($conditions,'id DESC');
        $this->pager = $e_user->spPager()->getPager();
        $scenerys = array();
        $i=0;
		while( $i <= count($this->pictureinfo)-1)
		{		  
		  $linkid = $this->pictureinfo[$i]['linkid'];
		  $conditions = array( 'id' => $linkid );
		  $sceneryinfo = spClass('sceneryinfo');
		  $scenerys[] = $sceneryinfo->find($conditions,"id DESC");
		  $i++;
		}
		$this->scenerys = $scenerys;

        // 昵称和头像向导
        if(empty($e_result["uname"])){
          $this->jump(spUrl('ucenter','UserGuide')); // 跳转到首页
        }

		$this -> display('user/UserPicture.html');
	}

	// 用户中心_动态
	function UserLog(){
		
		$id = $_SESSION["quser"]['uid'];
		$conditions = array( 'uid' => $id );
		$e_userinfo = spClass('lib_user');
        $e_result = $e_userinfo->find($conditions);
		$this->id = $id; 
		$this->uname = $e_result["uname"];
		$this->integral = $e_result["integral"];
		$this->avatar = str_replace(".jpg","",$e_result['avatar']); 

		$conditions = array( 'userid' => $_SESSION["quser"]['uid'] );
		$userhistory = spClass('userhistory');
		$this->userhistory = $userhistory->spPager($this->spArgs('page', 1), 15)->findAll($conditions,'id DESC');
        $this->pager = $userhistory->spPager()->getPager();
        // 昵称和头像向导
        if(empty($e_result["uname"])){
          $this->jump(spUrl('ucenter','UserGuide')); // 跳转到首页
        }
        //dump($this->userhistory);

		$this -> display('user/UserLog.html');
	}

	// 用户中心_测试
	function UserTest(){
		$this -> display('user/UserTest.html');
	}

    // 注册用户
	function adddata(){
		$code = $this->spArgs('code');
		$Email = file_get_contents("reguser/".$code);

		$conditions = "Email = '".$Email."'";
		$lib_user = spClass('lib_user'); 
        $result = $lib_user->find($conditions);
		
		$Random = mt_srand(mktime());
	    $Random = mt_rand();

		$newrow = array( // PHP的数组
		'upass' => $Random,
		'Email' => $Email
		);
		$lib_user->verifier = $lib_user->verifier_reg; // 切换验证规则
		$json = json_encode($lib_user->spVerifier($newrow));
		if(empty($result['Email'])){ 
		  $newrow['upass'] = md5($Random);
		  $lib_user->create($newrow); 
		}  // 进行新增操作


	  // 获取邮箱配置信息
          $emailhttp = spAccess('r','emailhttp');
          $emailport = spAccess('r','emailport');
          $emailid = spAccess('r','emailid');
          $emailpassword = spAccess('r','emailpassword');
          $MailServer = $emailhttp;      //SMTP 服务器
          $MailPort   = $emailport;					 //SMTP服务器端口号 默认25
          $MailId     = $emailid;  //服务器邮箱帐号
          $MailPw     = $emailpassword;			     //服务器邮箱密码
          /**
          *客户端信息
          */
          $domain = spAccess('r','domain');
          $Title      = '哪里玩网注册成功确认！';        //邮件标题
          $Content    = '尊敬的'.$Email.'用户<br />';
          $Content    = $Content.'我们已经将你的密码初始化为：'.$Random."<br />请你尽快使用以上密码登入网站并修改新密码.<br />登入地址为:http://".$domain."/login.html";        //邮件内容
          $email      = $Email;//接收者邮箱
          $smtp = new smtp($MailServer,$MailPort,true,$MailId,$MailPw);
          $smtp->debug = false;
          if($smtp->sendmail($email,$MailId, $Title, $Content, "HTML")){
            $this->success("恭喜你注册成功已经发送账号密码至".$Email."请尽快查收", spUrl("login","index"));
            unlink("reguser/".$code);
          } else {
            $this->error("抱歉发送失败，请重新再来一次！", spUrl("login","register"));
          }	

		echo($json);
	}
	
	// 编辑用户
	function editdata(){
		$data = $this->spArgs(); 
		$conditions = array('uid'=>$data['id']); 
		$lib_user = spClass('lib_user');
        $result = $lib_user->findAll($conditions); 
		$data['OldPass'] = $result[0]['upass'];
		if(!empty($data['Password']))
		{
			$row = array('Password' => $data['Password'],'checkPass' => md5($data['checkPass']),'OldPass' => $data['OldPass']);
			$lib_user = spClass('lib_user');
			$lib_user->verifier = $lib_user->verifier_ucmodify; // 切换验证规则
			$json = json_encode($lib_user->spVerifier($row));
			if($json == 'false'){ 
			  $row['upass'] = md5($data['Password']);
			  $lib_user->update($conditions,$row);
			}  // 进行新增操作
		}else{
			$row = array('uname' => $data['uname'],'realname' => $data['realname'],'avatar' => $data['avatar'],'Phone' => $data['Phone'],'QQ' => $data['QQ'],'Email' => $data['Email'],'provinceid'=>$data['provinceid'],'cityid'=>$data['cityid'],'areaid'=>$data['areaid'],'Address' => $data['Address']);
			$lib_user = spClass('lib_user');
			$lib_user->verifier = $lib_user->verifier_nopass; // 切换验证规则
			$json = json_encode($lib_user->spVerifier($row));
			if($json == 'false'){ 
			  $lib_user->update($conditions,$row);
			}  // 进行新增操作
		}
		echo($json);
		// 返回（显示）结果HTML，该结果由Smarty模板提供
		//$this -> display('mydata.html'); 
	}
	
	function subclass(){
		$data = $this->spArgs(); // $data是提交上来的数据
		
		$conditions = array( 'pid'=>$data['id'] );
		$c_name = spClass('lib_cityinfo'); 
		$data['cityinfo'] = $c_name->findAll($conditions); 

		echo json_encode($data);
	}

	// 退出登录
    function logout(){
		// 这里是PHP.net关于删除SESSION的方法
		unset($_SESSION['quser']);
		// 跳转回首页
		$this->success("已退出，返回首页！", spUrl("login","index"));
	}
}
